package com.ffcs.cn.action.base.checkbox;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.uicolorscheme.componentColors.DesktopComboBoxSchemeColor;
import com.supermap.desktop.core.utilties.CoreResources;
import org.jetbrains.userdefine.ui.speedSearch.SimpleColoredComponent;
import org.pushingpixels.substance.api.renderers.SubstanceDefaultComboBoxRenderer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialogJComboBoxMultiSelectClose extends SmDialog {
	private JLabel label;
	private JComboBox<ComboBoxSelectableDataItem> comboBox;
	// 带关闭comboBox编辑器，具体看ComboBoxMultiSelectCloseEditor类说明
	private ComboBoxMultiSelectCloseEditor comboBoxMultiSelectCloseEditor = null;
	// 记录comboBox下拉时的JList,方便添加数据后，刷新下拉被点击的数据项的状态
	private JList comboBoxList = null;


	public DialogJComboBoxMultiSelectClose() {
		//设置标题
		setTitle("JComboBox 多选示例");
		//设置初始化窗口大小
		Dimension dimension = new Dimension(300, 400);
		setSize(dimension);
		//设置最小窗口
		setMinimumSize(dimension);
		//初始化使用控件
		initComponents();
		//初始化布局，并添加控件
		initLayout();
	}

	private void initComponents() {
		this.label = new JLabel("多选与关闭:");

		this.comboBox = new JComboBox(){
			@Override
			public void setPopupVisible(boolean v) {
				// 重写setPopupVisible 什么都不做，解决掉comboBox下拉选择一项后，下拉菜单被关闭的问题
			}
		};
		this.comboBoxMultiSelectCloseEditor = new ComboBoxMultiSelectCloseEditor();
		this.comboBox.setRenderer(new SubstanceDefaultComboBoxRenderer() {
			@Override
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
				if (comboBoxList!=list){
					comboBoxList = list;// 让comboBoxList拿到新的JList
				}
				ComboBoxSelectableDataItem comboBoxSelectableDataItem = (ComboBoxSelectableDataItem) value;
				SimpleColoredComponent simpleColoredComponent = (SimpleColoredComponent) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
				simpleColoredComponent.setText(comboBoxSelectableDataItem.getValue());
				simpleColoredComponent.setOtherIconOnTheRightmost(true);// 开启simpleColoredComponent最右边支持显示图标

				if (comboBoxSelectableDataItem.isSelected()) {
					// 当数据被选中时，设置选中状态的前景色和背景色
					this.setForeground(DesktopComboBoxSchemeColor.instance.getSelectedForegroundColor());
					this.setBackground(DesktopComboBoxSchemeColor.instance.getSelectedBackgroundColor());
					// 设置最右边标识选中状态的图标
					simpleColoredComponent.setOtherIcon(CoreResources.getIcon("/coreresources/Image_SelectObject.png"));
				} else {
					// 当数据未被选中时，设置未选中状态的前景色和背景色
					this.setForeground(DesktopComboBoxSchemeColor.instance.getDefaultForegroundColor());
					this.setBackground(DesktopComboBoxSchemeColor.instance.getDefaultBackgroundColor());
					// 未选中，所以设置最右边图标为null
					simpleColoredComponent.setOtherIcon(null);
				}
				return this;
			}
		});
		this.comboBox.setEditor(this.comboBoxMultiSelectCloseEditor);
		this.comboBox.setEditable(true);// 开启编辑，方能通过自定义Editor，渲染带关闭的数据项目在comboBox框里，具体参见ComboBoxMultiSelectCloseEditor类说明
		// 添加ActionListener获取到在comboBox下拉框里点击的数据项，进而设置数据项的状态，并将数据项添加到Editor或从Editor移除
		this.comboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ComboBoxSelectableDataItem comboBoxSelectableDataItem = (ComboBoxSelectableDataItem) comboBox.getSelectedItem();
				comboBoxSelectableDataItem.setSelected(!comboBoxSelectableDataItem.isSelected());
				if (comboBoxSelectableDataItem.isSelected()) {
					addItemObject(comboBoxSelectableDataItem);
				}else{
					removeItemObject(comboBoxSelectableDataItem);
				}
				// 刷新下拉的JList
				if (comboBoxList != null) {
					comboBoxList.repaint();
				}
			}
		});
		// 添加数据
		for (int i = 0; i < 10; i++) {
			this.comboBox.addItem(new ComboBoxSelectableDataItem(String.valueOf( 10000+i)));
		}
	}

	private void initLayout() {
		//设置布局为 GridBagLayout 布局
		this.setLayout(new GridBagLayout());

		this.add(this.label, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0));
		this.add(this.comboBox, new GridBagConstraintsHelper(1, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setWeight(1, 0).setFill(GridBagConstraints.HORIZONTAL));

		this.add(new JPanel(), new GridBagConstraintsHelper(0, 1, 2, 1).setInsets(0, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setFill(GridBagConstraints.BOTH).setWeight(1, 1));
	}

	private void addItemObject(ComboBoxSelectableDataItem comboBoxSelectableDataItem) {
		PanelClose panelClose = new PanelClose(comboBoxSelectableDataItem){
			@Override
			protected void close(ComboBoxSelectableDataItem comboBoxSelectableDataItem) {
				// 响应点击关闭按钮后，从Editor移除
				removeItemObject(comboBoxSelectableDataItem);
				if (comboBoxList != null) {
					comboBoxList.repaint();
				}
			}
		};
		this.comboBoxMultiSelectCloseEditor.addPanelClose(panelClose);
	}

	private void removeItemObject(ComboBoxSelectableDataItem comboBoxSelectableDataItem) {
		this.comboBoxMultiSelectCloseEditor.removeComboBoxDataItem(comboBoxSelectableDataItem);
	}

}
